package hanjia;
import suanfa.ListNode;
/**
 * Description:
 * Author:    Dane
 * CreateTime:2025/2/4-下午9:02
 * Since  :   1.0
 * Version:   1.0
 */
public class LeetCode21 {

    public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode result=new ListNode();
        ListNode temp=result;

        while (list1!=null||list2!=null){
            int val1=Integer.MAX_VALUE;
            int val2=Integer.MAX_VALUE;
            if (list1!=null){
                val1=list1.val;

            }
            if (list2!=null){
                val2=list2.val;

            }
            if (val1<val2)
            {
                temp.next=new ListNode(val1,null);
                list1=list1.next;
            }else {
                temp.next=new ListNode(val2,null);
                list2=list2.next;
            }



        }
        return result.next;







    }

    public static ListNode mergeTwoLists2(ListNode list1, ListNode list2){

        if (list1==null){
           return list2;
        }
        if (list2==null){
            return list1;
        }


        if (list1.val<list2.val){
            list1.next=mergeTwoLists2(list1.next,list2);
            return list1;
        }else {
            list2.next=mergeTwoLists2(list1,list2.next);
            return list2;
        }


    }
}
